#!/bin/bash

# 成功校验
function check(){
    if [ $? -ne 0 ];then
        exit $1
    fi
}

tmp=$1
tag=${tmp// /}
git_version=$(git tag -n --sort=-taggerdate | grep $tag | head -1)
times_num=$2

git checkout -b $git_version

image_version=${git_version// /}
version="$image_version-$times_num"
# 构建版本号
echo $version

# 发布dubbo-interface接口包
cd dubbo-interface
mvn clean deploy -Denv.project.version=$version -DskipTests
check 1
cd ..

# 编译dubbo服务端应用
cd dubbo-service
mvn clean deploy -Denv.project.version=$version -P$tag -DskipTests
check 1
cd ..

# 编译dubbo应用
cd dubbo-web
mvn clean deploy -Denv.project.version=$version -P$tag -DskipTests
check 1
cd ..

cd dubbo-service
unzip ./target/dubbo-service.war -d www
docker build -t registry.yunlearn.org:5000/$tag/dubbo-service:$image_version .
docker push registry.yunlearn.org:5000/$tag/dubbo-service:$image_version
check 1
cd ..

cd dubbo-web
unzip ./target/dubbo-web.war -d www
docker build -t registry.yunlearn.org:5000/$tag/dubbo-web:$image_version .
docker push registry.yunlearn.org:5000/$tag/dubbo-web:$image_version
check 1
cd ..

# k8s集群部署dubbo应用脚本
echo '#!/bin/bash' > k8s-deploy.sh
echo kubectl scale deployment dubbo-service --replicas=0 >> k8s-deploy.sh
echo kubectl scale deployment dubbo-web --replicas=0 >> k8s-deploy.sh
dubboImage=registry.yunlearn.org:5000/$tag/dubbo-service:$image_version
dubboWebImage=registry.yunlearn.org:5000/$tag/dubbo-web:$image_version
echo "dubboImage: " $dubboImage
echo "dubboWebImage: " $dubboWebImage
sed "s#imageName#$dubboImage#g" -i dubbo-service-deploy.yaml
sed "s#imageName#$dubboWebImage#g" -i dubbo-web-deploy.yaml
echo kubectl apply -f mysql-endpoints.yaml >> k8s-deploy.sh
echo kubectl apply -f zookeeper-endpoints.yaml >> k8s-deploy.sh
echo kubectl apply -f dubbo-service-deploy.yaml >> k8s-deploy.sh
echo kubectl apply -f dubbo-web-deploy.yaml >> k8s-deploy.sh
echo kubectl apply -f dubbo-web-svc.yaml >> k8s-deploy.sh

